# frozen_string_literal: true

require 'oj'
require 'jwt'
# Applicaciont level helpers
module ApiHelpers
  def inject_params
    return if %w[OPTIONS GET].include? request.request_method

    @page_size = Config.current_page_size || 25
    @json_params = Oj.load(request.body.read, symbol_keys: true)
    @current_page = @json_params[:current_page] || 1
  end

  def auth_gate
    return if %w[OPTIONS GET].include? request.request_method

    token = env['HTTP_SECURE']
    halt 401 if token.nil? || token == ''
    secret = ENV['APP_SECRET']
    begin
      JWT.decode token, secret, true, algorithm: 'HS256'
    rescue StandardError
      halt 401
    end
  end
end
